﻿using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XS.Tunnel.Infrastructure.Extensions;
using Autodesk.Revit.UI.Selection;

namespace demo
{
    public class 打断
    {
        public static void Test(ExternalCommandData commandData)
        {

            拆分(commandData);
            return;


            var uidoc = commandData.Application.ActiveUIDocument;

            var doc = uidoc.Document;
            Selection selection = uidoc.Selection;


            using (Transaction t = new Transaction(doc, "cre111"))
            {
                t.Start();
                Reference refElement = selection.PickObject(ObjectType.PointOnElement, "请选择需要打断的点");
                Reference refElement1 = selection.PickObject(ObjectType.PointOnElement, "请选择第二个点");
                XYZ point = refElement.GlobalPoint;
                XYZ point1 = refElement1.GlobalPoint;
                Element elem = doc.GetElement(refElement);
                LocationCurve lc = elem.Location as LocationCurve;

                XYZ newPoint = lc.Curve.Project(point).XYZPoint;
                XYZ newPoint1 = lc.Curve.Project(point1).XYZPoint;

                XYZ startPoint = lc.Curve.GetEndPoint(0);
                XYZ endPoint = lc.Curve.GetEndPoint(1);

                Line l1 = Line.CreateBound(startPoint, GetNearPoint(startPoint, newPoint1, newPoint));
                Line l2 = Line.CreateBound(GetNearPoint(endPoint, newPoint, newPoint1), endPoint);
                Element el1 = doc.GetElement(ElementTransformUtils.CopyElement(doc, elem.Id, new XYZ(1, 0, 0)).First());
                Element el2 = doc.GetElement(ElementTransformUtils.CopyElement(doc, elem.Id, new XYZ(1, 0, 0)).First());
                (el1.Location as LocationCurve).Curve = l1;
                (el2.Location as LocationCurve).Curve = l2;
                doc.Delete(elem.Id);


                t.Commit();
            }
        }
        public static XYZ GetNearPoint(XYZ origin, XYZ pt1, XYZ pt2)
        {
            if (origin.DistanceTo(pt1) >= origin.DistanceTo(pt2))
            {
                return pt2;
            }
            else
            {
                return pt1;
            }
        }


        public static void 拆分(ExternalCommandData commandData)
        {

            UIApplication UIP = commandData.Application;
            Autodesk.Revit.ApplicationServices.Application APP = UIP.Application;
            UIDocument UIDOC = UIP.ActiveUIDocument;
            Autodesk.Revit.DB.Document doc = UIDOC.Document;
            Selection Sle = UIDOC.Selection;

            if (Sle.GetElementIds().ToList().Count > 0)
            {

                using (Transaction transaction = new Transaction(doc))
                {
                    transaction.Start("可见性");

                    FamilyInstance familyInstance = doc.GetElement(Sle.GetElementIds().ToList()[0] as ElementId) as FamilyInstance;

                    ParameterMap parameterSet = familyInstance.ParametersMap;

                    List<string> names = new List<string>();
                    foreach (var item in parameterSet)
                    {
                        Parameter parameterA = item as Parameter;
                        if (parameterA.Definition.ParameterGroup == BuiltInParameterGroup.INVALID)
                        {
                            names.Add((doc.GetElement(parameterA.Id) as ParameterElement).Name);

                        }
                    }

                    foreach (var name in names)
                    {

                        FamilyInstance familyInstanceA = doc.GetElement(ElementTransformUtils.CopyElement(doc, familyInstance.Id, new XYZ(0, 0, 0)).ToList()[0]) as FamilyInstance;

                        foreach (var n in names)
                        {
                            if (name == n)
                            {
                                familyInstanceA.GetParameters(name)[0].Set(1);
                            }
                            else
                            {
                                familyInstanceA.GetParameters(n)[0].Set(0);
                            }
                        }

                    }
                    doc.Delete(familyInstance.Id);

                    TaskDialog.Show("创建结果", "成功");
                    transaction.Commit();

                }

            }


        }



    }
}
